<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button
        @click="$router.push({name:'menu-create'})">
        添加菜单</el-button>
      </div>
      <el-table
      :data="tableData"
      style="width: 100%;">
      <el-table-column
        type="index" style="width: 90px;"
        label="编号">
      </el-table-column>
      <el-table-column
        prop="name"
        label="菜单名称">
      </el-table-column>
      <el-table-column
        prop="level"
        label="菜单级数">
      </el-table-column>
      <el-table-column
        prop="icon"
        label="前端图标">
      </el-table-column>
      <el-table-column
        prop="orderNum"
        label="排序">
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作">
        <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
// 引入获取所有菜单getAllMenu接口  通过解构因为menu里面有很多接口
// 引入删除菜单deleteMenu接口
import { getAllMenus, deleteMenu } from '@/services/menu'

export default {
  name: 'MenuIndex',
  data () {
    return {
      tableData: []
    }
  },
  created () {
    //  加载上级菜单信息
    this.loadAllMenus()
  },
  methods: {
    // 获取所有的菜单信息
    async loadAllMenus () {
      const { data } = await getAllMenus()
      // console.log(data)
      if (data.code === '000000') {
        this.tableData = data.data
      }
    },
    handleEdit (rowData) {
      // 跳转到编辑页面
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: rowData.id
        }
      })
    },
    handleDelete (rowData) {
      // console.log(rowData)
      // 删除的确认提示
      this.$confirm('确认删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 进行删除操作发送请求到服务器端进行删除
        const { data } = await deleteMenu(rowData.id)
        // console.log(data)
        if (data.code === '000000') {
          // 更新数据列表
          this.loadAllMenus()
          // 提示信息
          this.$message({
            type: 'success',
            message: '已删除成功!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
